package com.kawaiior.asoulworld.items.weapon;

import com.kawaiior.asoulworld.events.ASEventHelper;
import com.kawaiior.asoulworld.items.AsWeaponBase;
import com.kawaiior.asoulworld.util.EntityUtil;
import com.kawaiior.asoulworld.util.IDLGeneral;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.item.EnumAction;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import javax.annotation.Nonnull;
import java.util.List;


public class ItemCrystalRose extends AsWeaponBase {

    public ItemCrystalRose(String name) {
        super(name);
        this.max_damage = 1024;
    }

    @Nonnull
    @Override
    public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) {
        if (world.isRemote) return super.onItemRightClick(world, player, hand);

        ItemStack stack = player.getHeldItemMainhand();
        if (stack.getItem() == this)
        {
            for (int i = 0; i <128 ; i++) {
                if (ASEventHelper.teleportRandomly128(player.posX,player.posY,player.posZ,world,player)){
                    Vec3d basePos = player.getPositionVector();
                    float range = 3;
                    List<EntityLivingBase> entities = world.getEntitiesWithinAABB(EntityLivingBase.class, IDLGeneral.ServerAABB(basePos.addVector(-range, -range, -range), basePos.addVector(range, range, range)));
                    for (EntityLivingBase target: entities) {
                        if (target == player)continue;
                        EntityUtil.ApplyBuff(target, MobEffects.WITHER,2,5f);
                    }
                    player.getCooldownTracker().setCooldown(this, 200);
                    return ActionResult.newResult(EnumActionResult.SUCCESS, stack);
                }
            }
            return ActionResult.newResult(EnumActionResult.FAIL, stack);
        }
        return ActionResult.newResult(EnumActionResult.PASS, stack);
    }

    @Override
    public boolean getIsRepairable(ItemStack toRepair, ItemStack repair)
    {
        return false;
    }

    @Override
    public int getMaxItemUseDuration(ItemStack stack)
    {
        return 10;
    }

    @Override
    public EnumAction getItemUseAction(ItemStack stack) {
        return EnumAction.NONE;
    }

}
